"""
@file:maoyan_write_csv.py
@time:2023/09/18 0:12
@author:Jiajia Zhan
"""

import os

import pandas as pd

# 爬取的数据存储在csv中
def writeCSV():
    # 检查文件是否存在，如果存在就删除
    if os.path.exists('./maoyan.csv'):
        os.remove('./maoyan.csv')

    # 从文本文件中读取JSON数据
    with open('./maoyan.txt', 'r', encoding='utf-8') as file:
        data = [eval(line) for line in file]  # 将每行的JSON数据解析为字典

    # 创建DataFrame
    df = pd.DataFrame(data)

    # 将DataFrame保存为CSV文件
    df.to_csv('./maoyan.csv', index=False, encoding='utf-8')


# 程序主入口
if __name__ == '__main__':
    print("正在将数据写入CSV......")
    writeCSV()
    print("写入完成")